<?php

namespace task_model\controller;

use style\lib\Task_Ctlr;

/**
 * Created by 豆豆.
 * mail longrbl@163.com
 * User: Administrator
 * Date: 2018/1/15
 * Time: 14:23
 */
class IndexController extends Task_Ctlr
{
  /**
   * 实现在异步的task进程里面开启swoole多进程任务 来实现 比如批量上传数据到别的地方
   */
  function actionIndex()
  {
    $wor = '';
    echo time() . PHP_EOL;
    for ($i = 0; $i < 1000; $i++) {
      $process = new \swoole_process(function (\swoole_process $worker) {

        for ($i = 0; $i < 100000000000; $i++) {
          echo $i . PHP_EOL;
        }

      });
      $process->useQueue();
      $pid = $process->start();

      $wor[$pid] = $process;
    }


    while (1) {
      $ret = \swoole_process::wait();//回收进程
      if ($ret) {// $ret 是个数组 code是进程退出状态码，

      } else {
        break;
      }
    }

    echo time() . PHP_EOL;
  }

  /**
   * 来实现异步查询数据库的任务
   */
  function actionMysql()
  {
    $list = $this->service->db->createCommand("select * from {{%category}}")->queryAll();
    $this->service->finish(json_encode($list));
  }


}